using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Unitins.Component.PersistenceLayer.DataBase.SQL;

namespace Unitins.Component.Repository.Sepsi
{
    public class UsuarioManager : Unitins.Component.ComponentMaster.ManagerMaster<Usuario, UsuarioCollection>
    {
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static Usuario Buscar(Int64 idUsuario)
        {
            return Buscar(idUsuario, UsuarioAttribute.IdUsuario);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static UsuarioCollection Filtro(string valor, int campoFiltro, int campoOrdenacao, string tipoOrdenacao)
        {
            PersistenceLayer.DataBase.Parameter.PLSearchMounter<Usuario> myMounter = new Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounter<Usuario>();

            Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterSortType mySortType = Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterSortType.Ascending;

            if(tipoOrdenacao.ToLower().Equals("desc"))
                mySortType = Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterSortType.Descending;

            myMounter.Parameters.Add(new Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterParameter((UsuarioAttribute)campoFiltro, valor, Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterCompareType.Like, Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterConditionType.And));
            myMounter.SortCollection.Add(new Unitins.Component.PersistenceLayer.DataBase.Parameter.PLSearchMounterSort((UsuarioAttribute)campoOrdenacao, mySortType));

            return PersistenceLayer.Manager.SearchAll<Usuario, UsuarioCollection>(myMounter);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static UsuarioCollection BuscarAdministrador(int BuscarPor, string BuscarPorValue, int OrdenarPor, string AscDesc)
        {
            string Busca = "";
            if (BuscarPor == 1)
                Busca = "(tb_pessoa.nome LIKE '%" + BuscarPorValue + "%')";
            else
                if (BuscarPor == 2)
                    Busca = "(tb_usuario.login LIKE '%" + BuscarPorValue + "%')";
                else
                    Busca = "(tb_usuario.matricula LIKE '%" + BuscarPorValue + "%')";
            string Ordenar = "";
            if (OrdenarPor == 1)
                Ordenar = "tb_pessoa.nome " + AscDesc;
            else
                if(OrdenarPor == 2)
                    Ordenar = "tb_usuario.login " + AscDesc;
                else
                    Ordenar = "tb_usuario.matricula " + AscDesc;
            String sql = @"SELECT     tb_usuario.*
                            FROM         tb_pessoa INNER JOIN
                                                  tb_usuario ON tb_pessoa.id_pessoa = tb_usuario.id_pessoa INNER JOIN
                                                  tb_usuario_tipo ON tb_usuario.id_usuario_tipo = tb_usuario_tipo.id_usuario_tipo
                            WHERE tb_usuario_tipo.descricao = 'Administrador' and " + Busca + @"    
                            ORDER BY " +Ordenar;
            
            PLDataCommand command = new PLDataCommand(sql);
            return UsuarioManager.Pesquisar(command);

        }
    }
}
